﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using DirectUI.Mobile.UI.Native;

namespace DirectUI.Mobile.UI.Controls
{
    public class DirectUILabel : DirectUIControl
    {
        public DirectUILabel()
            : base()
        {
            this.Name = Guid.NewGuid().ToString();
        }

        protected override void InitControl()
        {
            stringFormat.FormatFlags = StringFormatFlags.NoWrap;
            stringFormat.Alignment = StringAlignment.Near;
            stringFormat.LineAlignment = StringAlignment.Near;
            BackColor = Color.Transparent;
        }

        public DirectUILabel(string text, int x, int y, int width, int height)
            : base()
        {
            this.text = text;
            this.Bounds = new Rectangle(x, y, width, height);
        }
        private string GetDisplayText()
        {  
            if (string.IsNullOrEmpty(Text))
            {
                return Text;
            }
            string[] aryTemp = Text.Split(new char[] { '\r' });
            string result = "";
            int index = 0;
            Graphics g = CreateGraphics();
            for (int i = 0; i < aryTemp.Length; i++)
            {
                if (i == 0)
                {
                    result = result + aryTemp[0][0];
                }
                else
                {
                    result = result + '\r' + aryTemp[0][0];
                }
                index = result.Length - 1;
                for (int j = 1; j < aryTemp[i].Length; j++)
                {
                    if (g.MeasureString(result.Substring(index) + aryTemp[i][j], this.Font).Width < this.Width)
                    {
                        result = result + aryTemp[i][j];
                    }
                    else
                    {
                        result = result + '\r' + aryTemp[i][j];
                        index = result.Length - 1;
                    }

                }
            }
            return result;
        }
        private string text = "";
        //public void SetTextWithOutRefresh(string value)
        //{
        //    text = value;
        //}
        /// <summary>
        /// Gets or sets the text of the control.
        /// </summary>
        public virtual string Text
        {
            get { return text ?? string.Empty; }
            set
            {
                if (text != value)
                {
                    text = value;
                    OnTextChanged();
                    OnPropertyChanged(new DirectUI.Mobile.UI.PropertyChangingEventArgs("Text"));
                   // this.Refresh();
                }
            }
        }
     
        protected StringFormat stringFormat = new StringFormat(StringFormatFlags.NoWrap);

        public StringAlignment Alignment
        {
            get { return stringFormat.Alignment; }
            set
            {
                if (Alignment != value)
                {
                    stringFormat.Alignment = value;
                    Invalidate();
                }
            }
        }

        public StringAlignment LineAlignment
        {
            get { return stringFormat.LineAlignment; }
            set
            {
                if (LineAlignment != value)
                {
                    stringFormat.LineAlignment = value;
                    Invalidate();
                }
            }
        }



        /// <summary>
        /// Occurs when the text property is changed.
        /// </summary>
        protected virtual void OnTextChanged()
        {
            Invalidate();
            if (TextChanged != null) TextChanged(this, EventArgs.Empty);
        }

        public event EventHandler TextChanged;



        public override void OnPaint(DirectUIPaintEventArgs e)
        {
            base.OnPaint(e);
            PaintText(e);
        }

        public Color ShadowColor { get; set; }

        private SolidBrush brush = new SolidBrush(Color.Black);

        public StringFormat Format { get { return stringFormat; } }

        protected void PaintDefaultText(DirectUIPaintEventArgs e, string text)
        {
            Graphics g = e.Graphics;
            Rectangle bounds = e.ControlBounds;
            bounds.Inflate(e.ScaleX(-3), e.ScaleY(-1));
            //bounds.Y += e.ScaleY(-1);
            if (bounds.Height > 0 && bounds.Width > 0)
            {
                if (ShadowColor.IsEmpty)
                {
                    brush.Color = Enabled ? ForeColor : Color.Silver;
                    g.DrawString(text, Font, brush, RectFFromRect(bounds), stringFormat);
                }
                else
                {
                    DirectUI.Mobile.UI.Drawing.GdiPlus.GdiExt.DrawStringShadow(g, text, Font, ForeColor, ShadowColor, bounds, stringFormat);
                }
             
            }
        }


        protected virtual void PaintText(DirectUIPaintEventArgs e)
        {
         
            if (!string.IsNullOrEmpty(Text))
            {
                string text = GetDisplayText();
                PaintDefaultText(e, text);
            }
        }
    }
}
